package com.erp.erp_ui.information.controller;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.warehouse.req.CustomerInfoReq;
import com.erp.erp_entitys.warehouse.req.CustomerQueryReq;
import com.erp.erp_entitys.warehouse.rsp.CustomerContactInfoDataRsp;
import com.erp.erp_entitys.warehouse.rsp.CustomerInfoDataRsp;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.information.req.AnnexAddReq;
import com.erp.erp_ui.information.req.BasicInfoReq;
import com.erp.erp_ui.information.service.BasicInformationService;
import com.erp.erp_ui.information.vo.BasicInfoVo;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName : BasicInformationController
 * @Description : 基础信息控制层
 * @Author : lst
 * @Date: 2021-03-19 17:46
 */
@Controller
@RequestMapping(value = "basicInformation")
public class BasicInformationController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BasicInformationController.class);

    /**
     * 业务逻辑接口
     */
    private final BasicInformationService basicInformationService;

    @Autowired
    public BasicInformationController(BasicInformationService basicInformationService) {
        this.basicInformationService = basicInformationService;
    }

    /**
     * 获取基础信息
     * @param req 指定获取哪些字典信息
     * @return 字典信息
     */
    @PostMapping(value = "getBasicInfo")
    @ResponseBody
    public R getBasicInfo(@RequestBody BasicInfoReq req) {
        try {
            LOGGER.info("基础信息-获取基础信息:req=" + req.toString());
            UserEntity user = (UserEntity) this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            long startTime=System.currentTimeMillis();
            BasicInfoVo basicInfo = basicInformationService.getBasicInfo(req, name, code);
            long endTime=System.currentTimeMillis();
            System.out.println("程序运行时间： "+(endTime-startTime)+"hs");
            return R.build(basicInfo);
        } catch (Exception e) {
            LOGGER.info("基础信息-获取基础信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取客户信息
     * @param info 查询参数
     * @return 客户信息
     */
    @GetMapping(value = "getCustomerInfo")
    @ResponseBody
    public R getCustomerInfo(String info) {
        try {
            LOGGER.info("基础信息-获取客户信息:info=" + info);
            BasicInfoVo basicInfo = basicInformationService.getCustomerInfo(info, null);
            return R.build(basicInfo);
        } catch (Exception e) {
            LOGGER.info("基础信息-获取客户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过条件获取客户信息
     *
     * @param req 查询条件实体
     * @return 客户信息
     */
    @GetMapping(value = "getCustomerByPage")
    @ResponseBody
    public R getCustomerByPage(CustomerQueryReq req) {
        try {
            LOGGER.info("基础信息-通过条件获取客户信息:req=" + req.toString());
            PageInfo<CustomerInfoDataRsp> pageInfo = basicInformationService.getCustomerByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("基础信息-通过条件获取客户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 根据客户编码获取客户联系人信息
     * @param req 客户编码
     * @return 客户联系人信息
     */
    @GetMapping(value = "getCustomerContactList")
    @ResponseBody
    public R getCustomerContactByPage(CustomerInfoReq req) {
        try {
            LOGGER.info("基础信息-根据客户编码获取客户联系人信息:req=" + req.toString());
            PageInfo<CustomerContactInfoDataRsp> pageInfo = basicInformationService.getCustomerContactByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("基础信息-根据客户编码获取客户联系人信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取自己的客户信息
     * @param info 查询参数
     * @return 客户信息
     */
    @GetMapping(value = "getCustomerOfSelf")
    @ResponseBody
    public R getCustomerOfSelf(String info) {
        try {
            LOGGER.info("基础信息-获取自己的客户信息:info=" + info);
            UserEntity user = (UserEntity) this.getBean("user");
            String code = user.getCode();
            BasicInfoVo basicInfo = basicInformationService.getCustomerInfo(info, code);
            return R.build(basicInfo);
        } catch (Exception e) {
            LOGGER.info("基础信息-获取自己的客户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 附件新增、修改
     * @param req 附件新增、修改入参
     * @return 新增、修改结果
     */
    @PostMapping("/annex/add")
    @ResponseBody
    public R add(@RequestBody AnnexAddReq req){
        try {
            LOGGER.info("基础信息-附件新增、修改 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return basicInformationService.annexAdd(req, name, code);
        }catch (Exception e){
            LOGGER.info("基础信息-附件新增、修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
